package com.mmlc.meimei.lichengkongjian.home;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterFragment;
import com.mmlc.meimei.lichengkongjian.Adapter.NewProductListAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.classify.ProductInfoActivity;
import com.mmlc.meimei.lichengkongjian.customview.SwipeRecyclerView;
import com.mmlc.network.filter.NewProductListRequestFilter;
import com.mmlc.network.responsebean.NewProductListResponseBean;
import com.mmlc.utils.AppUtils;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;


public class NewpoductListFragment extends InterFragment {

    @Override
    protected int initResourceID() {
        return R.layout.fragment_new_product;
    }

    SwipeRecyclerView mSwipeRecyclerView ;
    List<NewProductListResponseBean.DataBean.GoodslistBean> datas;
    NewProductListAdapter adapter;

    @Override
    protected void initView() {
        mSwipeRecyclerView = ViewUtils.getView(view, R.id.swipeRecyclerView);
        mSwipeRecyclerView.getSwipeRefreshLayout().setColorSchemeColors(Color.RED);
        GridLayoutManager manager = new GridLayoutManager(getActivity(),2);
        mSwipeRecyclerView.getRecyclerView().setLayoutManager(manager);
        datas = new ArrayList<>();
        adapter = new NewProductListAdapter(datas,getActivity());
        mSwipeRecyclerView.setAdapter(adapter);
        mSwipeRecyclerView.setRefreshEnable(false);
        mSwipeRecyclerView.setLoadMoreEnable(false);
        adapter.setmOnItemClickListener(new NewProductListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), ProductInfoActivity.class);
                intent.putExtra("name", datas.get(position).getGoods_name()).putExtra("goods_id", datas.get(position).getId());
                startActivity(intent);
            }
        });

    }



    @Override
    protected void initInfo() {
        super.initInfo();
        NewProductListRequestFilter filter=new NewProductListRequestFilter((BaseParentActivity) getActivity());
        filter.requestBean.recommend_id=getArguments().getString("id");
        filter.requestBean.version = AppUtils.getVersionCode(getActivity())+"";
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<NewProductListResponseBean>() {

            @Override
            public void onSuccess(NewProductListResponseBean result) {
                datas.clear();
                datas.addAll(result.getData().getGoodslist());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess.toString());

            }
        });

    }

}
